package com.eber.demo.design.chain;

// 抽象处理者
abstract class Logger {
    protected Logger nextLogger;

    public void setNextLogger(Logger nextLogger) {
        this.nextLogger = nextLogger;
    }

    public abstract void logMessage(int level, String message);
}

// 具体处理者
class ConsoleLogger extends Logger {
    public void logMessage(int level, String message) {
        if (level <= 1) {
            System.out.println("Console Logger: " + message);
        } else {
            nextLogger.logMessage(level, message);
        }
    }
}

class FileLogger extends Logger {
    public void logMessage(int level, String message) {
        if (level <= 2) {
            System.out.println("File Logger: " + message);
        } else {
            nextLogger.logMessage(level, message);
        }
    }
}

class ErrorLogger extends Logger {
    public void logMessage(int level, String message) {
        if (level <= 3) {
            System.out.println("Error Logger: " + message);
        } else {
            nextLogger.logMessage(level, message);
        }
    }
}

public class ChainApplication {
    public static void main(String[] args) {
        ConsoleLogger consoleLogger = new ConsoleLogger();
        FileLogger fileLogger = new FileLogger();
        ErrorLogger errorLogger = new ErrorLogger();

        consoleLogger.setNextLogger(fileLogger);
        fileLogger.setNextLogger(errorLogger);

        // 处理日志消息
        consoleLogger.logMessage(2, "This is an error message.");
    }
}
